package com.gt.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class MD5Utils {

//	/**
//	 * MD5纯加密，不用盐
//	 *
//	 * @param sourceString
//	 * @return
//	 */
//	public static String MD5Encode(String sourceString) {
//		String resultString = null;
//		try {
//			resultString = new String(sourceString);
//			MessageDigest md = MessageDigest.getInstance("MD5");
//			resultString = byte2hexString(md.digest(resultString.getBytes("utf-8")));
//		} catch (Exception localException) {
//		}
//		return resultString;
//	}
//
//	private static final String byte2hexString(byte[] bytes) {
//		StringBuffer bf = new StringBuffer(bytes.length * 2);
//		for (int i = 0; i < bytes.length; i++) {
//			if ((bytes[i] & 0xFF) < 16) {
//				bf.append("0");
//			}
//			bf.append(Long.toString(bytes[i] & 0xFF, 16));
//		}
//		return bf.toString();
//	}
	/**
	 * 固定盐值 用于h5用户密码加密
	 */
	public static String H5_SALT = "dLJEIR34!@#4382@!@";

	public static String MD5Encode(String sourceString) {
		String resultString = null;
		try {
			resultString = sourceString;
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byte2hexString(md.digest(resultString.getBytes(StandardCharsets.UTF_8)));
		} catch (Exception ignored) {
		}
		return resultString;
	}

	private static String byte2hexString(byte[] bytes) {
		StringBuilder bf = new StringBuilder(bytes.length * 2);
		for (byte aByte : bytes) {
			if ((aByte & 0xFF) < 16) {
				bf.append("0");
			}
			bf.append(Long.toString(aByte & 0xFF, 16));
		}
		return bf.toString();
	}
}